package com.hqyj.sale.controller;

import com.hqyj.sale.model.SaleInfo;
import com.hqyj.system.model.User;
import frame.sale.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author cz
 * 2021/9/6 9:23
 */
@Controller
@RequestMapping("/saleController")
public class SaleController extends BaseController {

    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        //1.接收数据;
        //3.调用业务;
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfo();
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr) {
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        //3.调用业务;
        SaleInfo saleInfo_db = saleInfoService.querySaleInfoById(id);
        System.out.println(saleInfo_db);
        model.addAttribute("saleInfo_db", saleInfo_db);
        //2.跳转页面;
        return "view/sale/updateSaleInfo";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr,SaleInfo saleInfo){
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        saleInfo.setProductId(id);

        //3.调用业务;
        int n = saleInfoService.updateSaleInfoBySaleInfo(saleInfo);

        //重新查询返回跳转
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfo();
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,SaleInfo saleInfo){
        //1.接收数据;

        //3.调用业务;
        int n = saleInfoService.addSaleInfoBySaleInfo(saleInfo);

        //重新查询返回跳转
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfo();
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoList";
    }

    @RequestMapping("/send.do")
    public String send(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr,SaleInfo saleInfo){
        //1.接收数据;
        int saleId = Integer.parseInt(idStr);
        saleInfo.setProductId(saleId);
        String type = "成品";
        //3.调用业务;
        int n = saleInfoService.updateSaleInfoById(saleId);
        SaleInfo saleInfo_db = saleInfoService.querySaleInfoById(saleId);
        int num = saleInfoService.insertOutRepertoryByProductInfo(saleInfo_db.getProductName(),saleInfo_db.getSaleAmount(),type);
        int m = orderInfoService.updateOrderInfoStateBySaleId(saleInfo_db.getOrderId());

        //重新查询返回跳转
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfo();
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoList";
    }

    @RequestMapping("/listSend.do")
    public String listSend(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model){
        //1.接收数据;
        //3.调用业务;
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfoBySended();

        //重新查询返回跳转
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoListSended";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr){
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        //3.调用业务;
        int n = saleInfoService.deleteSaleInfoById(id);

        //重新查询返回跳转
        List<SaleInfo> saleInfo_dbList = saleInfoService.queryAllSaleInfoBySended();
        model.addAttribute("saleInfo_dbList", saleInfo_dbList);
        //2.跳转页面;
        return "view/sale/saleInfoListSended";
    }
}
